#include "git-local-branch.h"

#include <cassert>

#include <git2/refs.h>
#include <git2/branch.h>

#include "util/logger/logger.h"

#include "git-ptr-holders.h"

namespace git
{

LocalBranch::LocalBranch(git_reference *ref)
    : Branch(ref) {
}

LocalBranch::LocalBranch(LocalBranch &&b)
    : Branch(std::move(b)) {
}

LocalBranch::~LocalBranch() {
}

LocalBranch &LocalBranch::operator=(LocalBranch &&b) {
    Branch::operator=(std::move(b));
    return *this;
}

Upstream LocalBranch::upstream() const {
    assert(ref_);

    RefHolder upref;
    git_branch_upstream(upref.visit(), ref_);
    if (!upref) {
        return Upstream();
    }

    if (git_reference_is_remote(upref.look())) {
        return RemoteTrackBranch(upref.take());
    } else {
        return LocalBranch(upref.take());
    }
}

} // namespace git
